import type { ILcCustomMessageItem } from './interface'
import type { ChatItem } from '@/types/app'

export const combineToNewChatList = (items: ChatItem[], localItems: ILcCustomMessageItem[]) => {
  if (!localItems.length)
    return items

  const localRecs = localItems.reduce<Record<string, ILcCustomMessageItem[]>>((acc, c) => {
    if (!acc[c.aid])
      acc[c.aid] = []
    acc[c.aid].push(c)
    return acc
  }, {})
  return items.reduce<ChatItem[]>((acc, c) => {
    acc.push(c)
    if (localRecs[c.id]) {
      localRecs[c.id].forEach((item, i) => {
        acc.push({
          // id: item.aid,
          id: item.id,
          type: 'custom',
          rawId: item.aid,
          content: {
            ...item.content,
          },
          agent_thoughts: [],
          isAnswer: true,
          message_files: [],
        })
      })
    }
    return acc
  }, [])
}

export const createChatItemFromLcItem = (item: ILcCustomMessageItem,count=0): ChatItem => {
  const [aid] = item.aid.split('$')
  return {
    id: `${aid}$${Date.now()+count}`,
    _key: `${aid}_${Date.now()+count}`,
    type: 'custom',
    rawId: aid,
    content: {
      ...item.content,
    },
    agent_thoughts: [],
    isAnswer: true,
    message_files: [],
  }
}
